
using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.DataStructure;
using Entity;

namespace AutoGear.BLL
{
    public class ShipmentCost
    {
        #region ShipmentCost Private Fields

        int _shipmentCostId;
        double _weightRangeFrom;
        double _weightRangeTo;
        string _weightUnit;
        double _shippingCostGround;
        double _shippingCostStandard;
        double _shippingCostExpress;
        double _shippingCostOvernight;
        bool _isActive;
        bool _isDeleted;
        DateTime _createDate;
        bool _isPercentage;
        #endregion

        #region ShipmentCost Constractor
        public ShipmentCost() { }

        public ShipmentCost
        (
            int shipmentCostId,
            double weightRangeFrom,
            double weightRangeTo,
            string weightUnit,
            double shippingCostGround,
            double shippingCostStandard,
            double shippingCostExpress,
            double shippingCostOvernight,
            bool isActive,
            bool isDeleted,
            DateTime createDate,
            bool isPerCentage
        )
        {
            this._shipmentCostId = shipmentCostId;
            this._weightRangeFrom = weightRangeFrom;
            this._weightRangeTo = weightRangeTo;
            this._weightUnit = weightUnit;
            this._shippingCostGround = shippingCostGround;
            this._shippingCostStandard = shippingCostStandard;
            this._shippingCostExpress = shippingCostExpress;
            this._shippingCostOvernight = shippingCostOvernight;
            this._isActive = isActive;
            this._isDeleted = isDeleted;
            this._createDate = createDate;
            this._isPercentage = isPerCentage;
        }

        public ShipmentCost
        (
          TblShipmentCost tblShipmentCost
        )
        {
            this._shipmentCostId = tblShipmentCost.ShipmentCostId;
            this._weightRangeFrom = Convert.ToDouble(tblShipmentCost.WeightRangeFrom);
            this._weightRangeTo = Convert.ToDouble(tblShipmentCost.WeightRangeTo);
            this._weightUnit = tblShipmentCost.WeightUnit;
            this._shippingCostGround = Convert.ToDouble(tblShipmentCost.ShippingCostGround);
            this._shippingCostStandard = Convert.ToDouble(tblShipmentCost.ShippingCostStandard);
            this._shippingCostExpress = Convert.ToDouble(tblShipmentCost.ShippingCostExpress);
            this._shippingCostOvernight = Convert.ToDouble(tblShipmentCost.ShippingCostOvernight);
            this._isActive = tblShipmentCost.IsActive;
            this._isDeleted = tblShipmentCost.IsDeleted;
            this._createDate = tblShipmentCost.CreateDate;
            this._isPercentage = tblShipmentCost.IsPercentange;

        }
        #endregion

        #region Public Properties
        public int ShipmentCostId
        {
            get { return _shipmentCostId; }
        }
        public double WeightRangeFrom
        {
            get { return _weightRangeFrom; }
            set { _weightRangeFrom = value; }
        }
        public double WeightRangeTo
        {
            get { return _weightRangeTo; }
            set { _weightRangeTo = value; }
        }
        public string WeightUnit
        {
            get { return _weightUnit; }
            set { _weightUnit = value; }
        }
        public double ShippingCostGround
        {
            get { return _shippingCostGround; }
            set { _shippingCostGround = value; }
        }

        public double ShippingCostStandard
        {
            get { return _shippingCostStandard; }
            set { _shippingCostStandard = value; }
        }

        public double ShippingCostExpress
        {
            get { return _shippingCostExpress; }
            set { _shippingCostExpress = value; }
        }

        public double ShippingCostOvernight
        {
            get { return _shippingCostOvernight; }
            set { _shippingCostOvernight = value; }
        }

        public bool IsActive
        {
            get { return this._isActive; }
            set { this._isActive = value; }
        }
        public bool IsDeleted
        {
            get { return this._isDeleted; }
            set { this._isDeleted = value; }
        }
        public DateTime CreateDate
        {
            get { return this._createDate; }
        }
        public bool IsPercentage
        {
            get { return this._isPercentage; }
            set { this._isPercentage = value; }
        }
        #endregion

        public void Create()
        {

            try
            {
                this._shipmentCostId = TblShipmentCost.CreateNewTblShipmentCost(Convert.ToDecimal(this._weightRangeFrom), Convert.ToDecimal(this._weightRangeTo), this._weightUnit, Convert.ToDecimal(this._shippingCostGround), Convert.ToDecimal(this._shippingCostStandard), Convert.ToDecimal(this._shippingCostExpress), Convert.ToDecimal(this._shippingCostOvernight), this._isActive, this._isDeleted, DateTime.Now,this._isPercentage);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool Update()
        {
            try
            {
                TblShipmentCost tblShipmentCost = TblShipmentCost.GetTblShipmentCostByShipmentCostId(this._shipmentCostId);
                tblShipmentCost.WeightRangeFrom = Convert.ToDecimal(this._weightRangeFrom);
                tblShipmentCost.WeightRangeTo = Convert.ToDecimal(this._weightRangeTo);
                tblShipmentCost.WeightUnit = this._weightUnit;
                tblShipmentCost.ShippingCostGround = Convert.ToDecimal(this._shippingCostGround);
                tblShipmentCost.ShippingCostStandard = Convert.ToDecimal(this._shippingCostStandard);
                tblShipmentCost.ShippingCostExpress = Convert.ToDecimal(this._shippingCostExpress);
                tblShipmentCost.ShippingCostOvernight = Convert.ToDecimal(this._shippingCostOvernight);
                tblShipmentCost.IsActive = this._isActive;
                tblShipmentCost.IsDeleted = this._isDeleted;
                tblShipmentCost.IsPercentange = this._isPercentage;
                return tblShipmentCost.Save();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void Delete()
        {
            /**************************************************
             * will update database table, will delete physical
             * files and will write in admin log about the 
             * delete
             **************************************************/
            this._isDeleted = true;
            this.Update();
        }

        public static void DeleteShipmentCost(List<int> idList)
        {
            TblShipmentCost.DeleteTblShipmentCostList(idList);
        }

        public static void DeleteShipmentCost(ReadWriteCustomCollection<ShipmentCost> ShipmentCost)
        {
            foreach (ShipmentCost shipmentCost in ShipmentCost)
            {
                shipmentCost.Delete();
            }
        }

        public static ShipmentCost Load(int shipmentCostId)
        {
            try
            {
                TblShipmentCost tmpTblShipmentCost = TblShipmentCost.GetTblShipmentCostByShipmentCostId(shipmentCostId);
                ShipmentCost shipmentCost = new ShipmentCost(tmpTblShipmentCost);
                return shipmentCost;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static ReadWriteCustomCollection<ShipmentCost> GetBackendShipmentCostList(bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblShipmentCost.GetAllTblShipmentCostPaged(orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblShipmentCostExtended.GetNonDeletedShipmentCostPaged(orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendShipmentCostListCount(bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblShipmentCost.GetAllTblShipmentCostPagedCount("", 0, 0);
            }
            else
            {
                return TblShipmentCostExtended.GetNonDeletedShipmentCostCount();
            }
        }

        public static ReadWriteCustomCollection<ShipmentCost> GetBackendShipmentCostListByWeightRange(double WeightRange, bool includeDeleted, string orderBy, int startIndex, int pageSize)
        {
            if (includeDeleted)
            {
                return LoadFromCustomCollection(TblShipmentCostExtended.GetAllShipmentCostByWeightRangePaged(Convert.ToDecimal(WeightRange), orderBy, startIndex, pageSize));
            }
            else
            {
                return LoadFromCustomCollection(TblShipmentCostExtended.GetNonDeletedShipmentCostByWeightRangePaged(Convert.ToDecimal(WeightRange), orderBy, startIndex, pageSize));
            }
        }

        public static int GetBackendShipmentCostListByWeightRangeCount(double WeightRange, bool includeDeleted)
        {
            if (includeDeleted)
            {
                return TblShipmentCostExtended.GetAllShipmentCostByWeightRangeCount(Convert.ToDecimal(WeightRange));
            }
            else
            {
                return TblShipmentCostExtended.GetNonDeletedShipmentCostByWeightRangeCount(Convert.ToDecimal(WeightRange));
            }
        }

        public static bool IsWeightRangeValid(double weightRange, string weightUnit)
        {
            ReadWriteCustomCollection<ShipmentCost> shipmentCost = ShipmentCost.GetBackendShipmentCostListByWeightRange(weightRange, false, "WeightRangeFrom", 0, 999999);
            if (shipmentCost.Count > 0)
            {
                foreach (ShipmentCost tmp in shipmentCost)
                {
                    if (tmp.WeightUnit == weightUnit)
                    {
                        return false;
                    }
                }
            }
            else
            {
                return true;
            }
            return true;
        }

        public static bool IsWeightRangeValid(double weightRange, string weightUnit, int shipmentCostId)
        {
            ReadWriteCustomCollection<ShipmentCost> shipmentCost = ShipmentCost.GetBackendShipmentCostListByWeightRange(weightRange, false, "WeightRangeFrom", 0, 999999);
            if (shipmentCost.Count > 0)
            {
                foreach (ShipmentCost tmp in shipmentCost)
                {
                    if (tmp.WeightUnit == weightUnit && tmp.ShipmentCostId != shipmentCostId)
                    {
                        return false;
                    }
                }
            }
            else
            {
                return true;
            }
            return true;
        }

        private static ReadWriteCustomCollection<ShipmentCost> LoadFromCustomCollection(ECommerce.DAL.CustomCollection<TblShipmentCost> tblShipmentCost)
        {
            ReadWriteCustomCollection<ShipmentCost> result = new ReadWriteCustomCollection<ShipmentCost>();
            for (int i = 0; i < tblShipmentCost.Count; i++)
            {
                TblShipmentCost tmpTblShipmentCost = tblShipmentCost[i];
                result.Add(new ShipmentCost(tmpTblShipmentCost));
            }
            return result;
        }

    }
}
